在上一篇文章中,我們完成魔術方塊的建立及旋轉,今天我們要來完成魔術方塊的偵測。
目錄
魔術方塊的偵測
讀取面狀態
建立CubeState()用來儲存魔術方塊目前狀態,新增List分別儲存各面物件
public List<GameObject> front = new List<GameObject>();
首先要在Scene中新增好射線起始位置,新增Empty GameObject,並將外觀設定成點。
並且為魔術方塊的每個面都新增一個射線,並將其位置放在各面的正上方。
將這些射線放在群組下(Rays)。
建立射線程式:
public Transform tUp;
public GameObject empty; //射線起始的位置
List<GameObject> BuildRays(Transform rayTransform, Vector3 direction)
{
int rayCount = 0;
List<GameObject> rays = new List<GameObject>();
for (int y = 1; y > -2; y--)
{
for (int x = -1; x < 2; x++)
{
Vector3 startPos = new Vector3( rayTransform.localPosition.x + x,
rayTransform.localPosition.y + y,
rayTransform.localPosition.z);
GameObject rayStart = Instantiate(empty, startPos, Quaternion.identity, rayTransform);
rayStart.name = rayCount.ToString(); //射線編號
rays.Add(rayStart);//新增射線
rayCount++;
}
}
rayTransform.localRotation = Quaternion.Euler(direction);
return rays;
}
為魔術方塊的不同面,分別建立3x3的射線
void SetRayTransforms()
{
upRays = BuildRays(tUp, new Vector3(90, 90, 0));
downRays = BuildRays(tDown, new Vector3(270, 90, 0));
leftRays = BuildRays(tLeft, new Vector3(0, 180, 0));
rightRays = BuildRays(tRight, new Vector3(0, 0, 0));
frontRays = BuildRays(tFront, new Vector3(0, 90, 0));
backRays = BuildRays(tBack, new Vector3(0, 270, 0));
}
為了防止射線偵測到其他物體,需要新增一個圖層,並將各魔術方塊的面圖層都改成該圖層。
並在程式碼中新增圖層遮罩。
private int layerMask = 1 << 8;
讀取面程式運用到Unity中的Physics.Raycast
public List<GameObject> ReadFace(List<GameObject> rayStarts, Transform rayTransform)
{
List<GameObject> facesHit = new List<GameObject>();
foreach (GameObject rayStart in rayStarts)
{
Vector3 ray = rayStart.transform.position;
RaycastHit hit;
if (Physics.Raycast(ray, rayTransform.forward, out hit, Mathf.Infinity, layerMask))
{
Debug.DrawRay(ray, rayTransform.forward * hit.distance, Color.yellow);
facesHit.Add(hit.collider.gameObject);
}
else
{
Debug.DrawRay(ray, rayTransform.forward * 1000, Color.green);
}
}
return facesHit;
}
使用ReadState讀取各面目前狀況
public void ReadState()
{
cubeState = FindObjectOfType<CubeState>();
cubeState.up = ReadFace(upRays, tUp);
cubeState.down = ReadFace(downRays, tDown);
cubeState.left = ReadFace(leftRays, tLeft);
cubeState.right = ReadFace(rightRays, tRight);
cubeState.front = ReadFace(frontRays, tFront);
cubeState.back = ReadFace(backRays, tBack);
}
以上就是魔術方塊的偵測,下一篇會是魔術方塊面的旋轉,明天見!